<?php
 
  class Order_m extends CI_Model
  {
      
      function __construct()
      {
         parent::__construct();
         $this->load->database();
     }
        
     function insert($arr)
     {
         return $this->db->insert('order',$arr);
     }  
     
     function update($where,$arr)
     {  
         $this->db->where($where);
         $data = $this->db->update('order',$arr);
         return  $data;
     }
            
     function delete($where)
     {
         $this->db->where($where);
         $this->db->delete('order');
     }
            
     
     //正序 全查         
     function select($where='',$order_by='',$limit='',$offset='')
     {  
         // where 条件
         if($where){
          $this->db->where($where);  
         }
         
         // order_by 条件
         if($order_by){
          $this->db->order_by($order_by);
         }
         // limit 条件
         if($offset&&$limit){
            $this->db->limit($limit,$offset);
         }
         if(!$offset&&$limit){
          $this->db->limit($limit);
         }
         
         $this->db->select('*');
         $data = $this->db->get('order');
         return  $data->result_array();
     }

     function getOrderCount($where){
       if($where){
          $this->db->where($where);
         }
        $this->db->select('orderid');
        $orderid = $this->db->get('order');
        $orderid = $orderid->result_array();
        return count($orderid);
     }

    function getOrderList($where,$page,$limit){
       if($where){
          $this->db->where_in('device_id',$where);
         }
         $this->db->where(array('paystate'=>1));
        $this->db->order_by('orderid desc');
        $offset=($page-1)*$limit;
        $this->db->limit($limit,$offset);
        $data = $this->db->get('order');
        $data = $data->result_array();
        return $data;
     }

     function getLastOrder($device_sn){
        $this->db->where(array('device_id'=>$device_sn,'paystate'=>1));           
        $this->db->order_by('orderid asc');
        $this->db->limit('1');
         
         $this->db->select('starttime,goods_on');
         $data = $this->db->get('order');
         $data=$data->result_array();
         if($data){
            $data=$data[0];
            $this->db->where(array('package_id'=>$data['goods_on']));
            $this->db->order_by('id asc');
            $this->db->limit('1');             
            $this->db->select('flow');
            $res = $this->db->get('recharge');
            $res=$res->result_array();
            $return['addtime']=$data['starttime'];
			if($res){
				$return['flow']=$res[0]['flow'];
			}
            
         }else{
            $return['flow']="未激活";
            $return['addtime']="未激活";
         }
         return  $return;
     }
     
 }
 ?>